<template>
	<view class="container">
		<view class="label">店员名称</view>
		<view class="form-item">
			<input type="text" v-model="staff.username" placeholder="请输入店员名称" class="input"></input>
		</view>
		<view class="label" style="margin-top: 20rpx;">店员手机号</view>
		<view class="form-item">
			<input type="number" v-model="staff.mobile" placeholder="请输入店员手机号" class="input"></input>
		</view>
		<button type="primary" class="save-btn" @click="saveStaff">保存</button>
	</view>
</template>

<script>
	import {
		createStoreuser,
		editStoreuser
	} from "@/api/userApi/user.js"
	export default {
		data() {
			return {
				staff: {
					username: "",
					mobile: "",
					store_id: ""
				},
				type: null
			};
		},
		methods: {
			saveStaff() {
				if (this.staff.username == '') {
					uni.showToast({
						icon: "none",
						title: "请输入店员名称"
					})
					return
				}
				if (this.staff.mobile == '' || this.staff.mobile.length != 11) {
					uni.showToast({
						icon: "none",
						title: "请检查手机号"
					})
					return
				}
				if (this.type == 1) {
					createStoreuser({
						username: this.staff.username,
						mobile: this.staff.mobile,
						store_id: this.staff.store_id
					}).then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: '保存成功',
								icon: 'success'
							});
							setTimeout(res => {
								uni.navigateBack(1)
							}, 1000)
						}
					})
				} else if (this.type == 2) {
					editStoreuser({
						username: this.staff.username,
						mobile: this.staff.mobile,
						id: this.staff.id,
						store_id: this.staff.store_id
					}).then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: '保存成功',
								icon: 'success'
							});
							setTimeout(res => {
								uni.navigateBack(1)
							}, 1000)
						}
					})
					uni.showToast({
						title: '编辑成功',
						icon: 'success'
					});
				}

			}
		},
		onLoad(options) {
			this.type = options.type
			if (options.type == 1) {
				this.staff.store_id = options.store_id
			}
			if (options.type == 2) {
				this.staff = JSON.parse(decodeURIComponent(options.val));
			}
		}
	};
</script>

<style lang="scss">
	.container {
		padding: 20rpx;
		height: 100vh;
		background-color: #f5f5f5;

		.label {
			font-size: 26rpx;
			color: #333;
			margin-bottom: 20rpx;
		}

		.form-item {
			display: flex;
			flex-direction: column;
			border-radius: 10rpx;
			background-color: #fff;

			.input {
				width: 70%;
				margin-top: 20rpx;
				border: none;
				border-radius: 10rpx;
				padding: 15rpx;
				font-size: 28rpx;
			}
		}

		.save-btn {
			position: fixed;
			bottom: 20rpx;
			left: 50%;
			transform: translateX(-50%);
			width: 600rpx;
			height: 80rpx;
			border-radius: 40rpx;
			font-size: 28rpx;
			color: #fff;
			background-color: #3385FF;
		}
	}
</style>